using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.WorkItemExtention.Properties;
using TXLooker.WorkItemExtention.WIQL;

namespace TXLooker.WorkItemExtention.Core
{
    /// <summary>
    /// 
    /// </summary>
    internal class OlWIQueryFolderView : OlFolderView
    {
        private static string MainViewPattern =
              @"<?xml version=""1.0""?>
                <view type=""table"">
                    <viewname>@viewname</viewname>
                    <viewstyle>table-layout:fixed;width:100%;font-family:Tahoma;font-style:normal;font-weight:normal;font-size:8pt;color:Black;font-charset:204</viewstyle>
                    <viewtime>0</viewtime>
                    <linecolor>8421504</linecolor>
                    <linestyle>3</linestyle>
                    <autosizing>0</autosizing>
                    <usequickflags>0</usequickflags>
                    <collapsestate></collapsestate>
                    <rowstyle>background-color:#FFFFFF</rowstyle>
                    <headerstyle>background-color:#D3D3D3</headerstyle>
                    <previewstyle>color:Blue</previewstyle>
                    <arrangement>
                        <autogroup>1</autogroup>
                        <collapseclient>01000000</collapseclient>
                    </arrangement>
                    @columns
                    @order
                    <groupbydefault>2</groupbydefault>
                    @previewpane
                </view>";

        private static string PreviewPaneNodePattern = 
                @"<previewpane>
                    <visible>{0}</visible>
                    <markasread>0</markasread>
                  </previewpane>";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="columns">Columns for View</param>
        /// <param name="viewName">Unique view name</param>
        /// <param name="bShowPreviewPane">Show Preview Pane in the Outlook Folder View</param>
        /// <returns>XML string for Outlook Folder View</returns>
        public static string CreateViewXML( string columnsXML, string viewName, bool bShowPreviewPane )
        {
            string previewpane = string.Format( PreviewPaneNodePattern, Convert.ToInt16( bShowPreviewPane ) );
            return MainViewPattern.Replace( "@viewname", viewName ).Replace( "@columns", columnsXML ).Replace( "@previewpane", previewpane ).Replace( "@order", string.Empty );
        }

        public OlWIQueryFolderView()
        {
            this.ShowPreviewPane = false;
            this.WebViewOn = false;
            this.WebViewURL = null;
            this.OlViewType = OlViewType.olTableView;
            this.OlViewName = Resources.OL_WORKITEM_VIEW_NAME;
        }
        public OlWIQueryFolderView( IEnumerable<OlWIQueryColumn> columns ) : this()
        {
            this.Init(columns);
        }
        public OlWIQueryFolderView( IEnumerable<WorkItemFieldDefinition> fields ) : this()
        {
            this.Init(fields);
        }
        public OlWIQueryFolderView( IEnumerable<FieldDefinition> fields ) : this()
        {
            this.Init(fields);
        }
        public OlWIQueryFolderView( WorkItemCollection items ) : this()
        {
            this.Init(items);
        }
        public void Init( WorkItemCollection workitems )
        {
            lock( this.m_columns )
            {
                this.m_columns.Clear();

                foreach( FieldDefinition item in workitems.DisplayFields )
                {
                    OlWIQueryColumn column = new OlWIQueryColumn( item, OlWIQueryColumn.GetOlFieldName( item.Name ) );
                    this.m_columns.Add( column );
                }
            }
        }
        public void Init( IEnumerable<WorkItemFieldDefinition> fields )
        {
            lock( this.m_columns )
            {
                this.m_columns.Clear();

                foreach( WorkItemFieldDefinition item in fields )
                {
                    OlWIQueryColumn column = new OlWIQueryColumn( item, OlWIQueryColumn.GetOlFieldName( item.Name ) );
                    this.m_columns.Add( column );
                }
            }
        }
        public void Init( IEnumerable<FieldDefinition> fields )
        {
            lock( this.m_columns )
            {
                this.m_columns.Clear();

                foreach( FieldDefinition item in fields )
                {
                    OlWIQueryColumn column = new OlWIQueryColumn( item, OlWIQueryColumn.GetOlFieldName(item.Name) );
                    this.m_columns.Add( column );
                }
            }
        }
        public void Init( IEnumerable<OlWIQueryColumn> columns )
        {
            lock( this.m_columns )
            {
                this.m_columns.Clear();
                this.m_columns.AddRange( columns );
            }
        }
        public override WIObjectType WIObjectType
        {
            get { return WIObjectType.Query; }
        }

        public IEnumerable<OlWIQueryColumn> Columns
        {
            get
            {
                lock( this.m_columns )
                {
                    foreach( OlWIQueryColumn item in this.m_columns )
                        yield return item;
                }
            }
        }
        private readonly List<OlWIQueryColumn> m_columns = new List<OlWIQueryColumn>();

        public bool ShowPreviewPane
        {
            get { return this.m_bShowPreviewPane;  }
            set { this.m_bShowPreviewPane = value; }
        }
        private bool m_bShowPreviewPane = false;

        public string XML
        {
            get 
            {
                StringBuilder builder = new StringBuilder();

                foreach( OlWIQueryColumn column in this.Columns )
                    builder.AppendLine( column.XML );

                return CreateViewXML( builder.ToString(), this.OlViewName, this.ShowPreviewPane );
            }
        }

        public IEnumerable<WorkItemFieldDefinition> ViewFields
        {
            get
            {
                lock( this.Columns )
                {
                    foreach( OlWIQueryColumn item in this.Columns )
                        yield return item.WIFieldDef;
                }
            }
        }

    }
}
